package me.keiwu.user.controller.rpc;

import com.alibaba.fastjson.JSON;

import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.service.UserInfoBean;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.pub.util.DateUtil;
import me.keiwu.user.domain.User;
import me.keiwu.user.domain.UserForm;
import me.keiwu.user.domain.UserStep;
import me.keiwu.user.service.CommentService;
import me.keiwu.user.service.ItemService;
import me.keiwu.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kei on 14/11/2017.
 */
@RestController
@RequestMapping("/rpc/user")
public class UserRpcController {

	private static final Logger logger = LoggerFactory.getLogger(UserRpcController.class);


    @Autowired
    private UserService userService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private CommentService commentService;

	// 个人信息
	@ResponseBody
	@RequestMapping("/info")
	public UserInfoBean userInfo(Long userId) {
		return userService.getUserInfo(userId);
	}

	@RequestMapping("/info/cache")
	public UserInfoBean userInfoCache(@RequestParam("userId") Long userId) {
		return userService.getUserInfoCache(userId);
	}

	@RequestMapping("/list")
	public List<User> getUserList(String idList) {
		List<Long> ids = JSON.parseArray(idList, Long.class);
		if (ids == null || ids.isEmpty())
			return null;
		return userService.getUsersByIdList(ids);
	}

	@RequestMapping("/login/daily")
	public Page<User> dailyLoginUser(@RequestParam(required = false) String nickName,
			@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
		return userService.getDailyLoginUser(nickName, page, size);
	}

	@RequestMapping("/login/history")
	public Page<User> historyLoginUser(@RequestParam(required = false) String nickName,
			@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
		return userService.getAllLoginUser(nickName, page, size);
	}

	@ResponseBody
	@RequestMapping("/gold/get")
	public Integer getUserGold(Long userId) {
		UserInfoBean user = userService.getUserInfo(userId);
		return user.getGold();
	}

	@ResponseBody
	@RequestMapping("/gold/update")
	public boolean updateGold(Long userId, Integer newGold) {
		logger.info("gold change, userId={}, newGold={}", userId, newGold);
		return userService.updateGold(userId, newGold);
	}

	@ResponseBody
	@RequestMapping("/gold/change")
	public boolean changeGold(Long userId, Integer gold) {
		logger.info("gold change, userId={}, gold={}", userId, gold);
		return userService.changeGold(userId, gold);
	}

	@ResponseBody
	@RequestMapping("/steps/scope")
	public Integer userStepsScope(Long userId, Integer fromDays, Integer toDays) {
		List<UserStep> steps = userService.getUserSteps(userId, fromDays, toDays);
		int count = 0;
		Set<Integer> days = new HashSet<>();
		for (UserStep step : steps) {
			Integer day = DateUtil.getDayOfYear(new Date(step.getTimestamp() * 1000L));
			if (days.contains(day))
				continue;
			days.add(day);
			count += step.getStep();
		}
		return count;
	}

	@ResponseBody
	@RequestMapping("/steps/daily")
	public Integer userStepsDaily(Long userId) {
		UserStep step = userService.getDailyUserStep(userId);
		return step == null ? 0 : step.getStep();
	}

	@ResponseBody
	@RequestMapping("findWithoutUsers")
	public Page<User> findWithoutUsers(String withoutList, String nickName, Integer page, Integer size) {
		return userService.findWithoutUsers(JSON.parseArray(withoutList, Long.class), nickName, page, size);
	}

	@ResponseBody
    @RequestMapping("hasComment")
    public boolean hasComment(Long userId, Long otherId) {
        return commentService.getComment(userId, otherId) != null;
    }
	
	@ResponseBody
	@RequestMapping("/userForm/saveUserForm")
	public ResponseV2 saveUserForm(Long userId, String formId, String action) {
		UserForm userForm = new UserForm();
		userForm.setUserId(userId);
		userForm.setFormId(formId);
		userForm.setAction(action);
		userService.saveUserForm(userForm);
		return ResponseV2.ok("保存成功");
	}

	@ResponseBody
	@RequestMapping("/userForm/findUserForm")
	public UserForm getUserForm(Long userId) {
		return userService.getOldestUserFormById(userId);
	}

	@ResponseBody
	@RequestMapping("/userForm/deleteExpiredForm")
	public boolean deleteExpiredForm() {
		return userService.deleteExpiredForm();
	}

	@ResponseBody
	@RequestMapping("/userForm/updateUsedForm")
	public boolean updateUsedForm(String formId) {
		return userService.updateUsedForm(formId);
	}

	@ResponseBody
	@RequestMapping("/userForm/deleteUsedForm")
	public boolean deleteUsedForm(Long id) {
		return userService.deleteUsedForm(id);
	}

	@ResponseBody
	@RequestMapping("/inactiveUsers")
	public Page<User> getAllInactiveUsers(Integer page, Integer size){ return userService.getInactiveUser(page, size); }

}
